// packageI/ecologicalPoint/ecologicalPointDetailed/ecologicalPointDetailed.js
const app = getApp();
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    //余额字样
    balanceLang: "生态积分",
    statusValue: 0,
    // 筛选类型 弹窗
    selectShow: false,
    selectType: 0,
    record_list: [],
    // 筛选类型数组
    serviceTypeList: [],
    selectTapInd: 0,

    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.page >= this.data.total_page) {
      return
    } else {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let urlStr = app.getNetAddresss("plugin.ecological-point.frontend.modules.point.controllers.records.index");
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.page,
          source_id: this.data.selectType,
          change_type: this.data.statusValue
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            let nextPageData = res.data.page_list.data;
            this.data.record_list = this.data.record_list.concat(nextPageData);
            this.setData({
              record_list: this.data.record_list
            })
          } else {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1000,
            });
          }
        },
        fail: (res) => {
          console.log(res);
        },
      });
    }

  },
  selectShowTap() {
    this.setData({
      selectShow: !this.data.selectShow
    })
  },
  selectStatus(e) {
    console.log(e)
    this.init();
    this.setData({
      statusValue: e.currentTarget.dataset.key,
    })
    this.getData();
  },
  selectTap(e) {
    let ind = e.currentTarget.dataset.ind;
    this.setData({
      selectType: ind,
      selectTapInd: ind,
      selectShow: false
    })
    this.init();
    this.getData();
  },
  init() {
    this.setData({
      isLoadMore: true,
      page: 1,
      total_page: 0,
      record_list: [],
    })
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.ecological-point.frontend.modules.point.controllers.records.index");
    app._postNetWork({
      url: urlStr,
      data: {
        page: this.data.page,
        source_id: this.data.selectType,
        change_type: this.data.statusValue
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          let basic_info = wx.getStorageSync('yz_basic_info');
          wx.setNavigationBarTitle({
            title: basic_info.ecological_point ? `${basic_info.ecological_point}明细` : '生态积分明细',
          })
          this.data.serviceTypeList = res.data.source_comment;
          this.data.serviceTypeList[0] = "业务类型";
          this.setData({
            record_list: res.data.page_list.data,
            source_comment: res.data.source_comment,
            isLoadMore: true,
            total_page: res.data.page_list.last_page,
            serviceTypeList: this.data.serviceTypeList
          })
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: this.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  }
})